//
//  SquareDetailsPost.m
//
//  Created by lianchuang  on 14-12-17
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "SquareDetailsPost.h"
#import "SquareDetailsQuote.h"


NSString *const kSquareDetailsPostMessage = @"message";
NSString *const kSquareDetailsPostAuthor = @"author";
NSString *const kSquareDetailsPostUseip = @"useip";
NSString *const kSquareDetailsPostAuthorid = @"authorid";
NSString *const kSquareDetailsPostPic = @"pic";
NSString *const kSquareDetailsPostQuotecount = @"quotecount";
NSString *const kSquareDetailsPostAvatar = @"avatar";
NSString *const kSquareDetailsPostDateline = @"dateline";
NSString *const kSquareDetailsPostQuote = @"quote";
NSString *const kSquareDetailsPostPid = @"pid";
NSString *const kSquareDetailsPostTid = @"tid";
NSString *const kSquareDetailsPostStatus = @"status";


@interface SquareDetailsPost ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SquareDetailsPost

@synthesize message = _message;
@synthesize author = _author;
@synthesize useip = _useip;
@synthesize authorid = _authorid;
@synthesize pic = _pic;
@synthesize quotecount = _quotecount;
@synthesize avatar = _avatar;
@synthesize dateline = _dateline;
@synthesize quote = _quote;
@synthesize pid = _pid;
@synthesize tid = _tid;
@synthesize status = _status;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.message = [self objectOrNilForKey:kSquareDetailsPostMessage fromDictionary:dict];
            self.author = [self objectOrNilForKey:kSquareDetailsPostAuthor fromDictionary:dict];
            self.useip = [self objectOrNilForKey:kSquareDetailsPostUseip fromDictionary:dict];
            self.authorid = [self objectOrNilForKey:kSquareDetailsPostAuthorid fromDictionary:dict];
            self.pic = [self objectOrNilForKey:kSquareDetailsPostPic fromDictionary:dict];
            self.quotecount = [self objectOrNilForKey:kSquareDetailsPostQuotecount fromDictionary:dict];
            self.avatar = [self objectOrNilForKey:kSquareDetailsPostAvatar fromDictionary:dict];
            self.dateline = [self objectOrNilForKey:kSquareDetailsPostDateline fromDictionary:dict];
    NSObject *receivedSquareDetailsQuote = [dict objectForKey:kSquareDetailsPostQuote];
    NSMutableArray *parsedSquareDetailsQuote = [NSMutableArray array];
    if ([receivedSquareDetailsQuote isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSquareDetailsQuote) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSquareDetailsQuote addObject:[SquareDetailsQuote modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSquareDetailsQuote isKindOfClass:[NSDictionary class]]) {
       [parsedSquareDetailsQuote addObject:[SquareDetailsQuote modelObjectWithDictionary:(NSDictionary *)receivedSquareDetailsQuote]];
    }

    self.quote = [NSMutableArray arrayWithArray:parsedSquareDetailsQuote];
            self.pid = [self objectOrNilForKey:kSquareDetailsPostPid fromDictionary:dict];
            self.tid = [self objectOrNilForKey:kSquareDetailsPostTid fromDictionary:dict];
            self.status = [self objectOrNilForKey:kSquareDetailsPostStatus fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.message forKey:kSquareDetailsPostMessage];
    [mutableDict setValue:self.author forKey:kSquareDetailsPostAuthor];
    [mutableDict setValue:self.useip forKey:kSquareDetailsPostUseip];
    [mutableDict setValue:self.authorid forKey:kSquareDetailsPostAuthorid];
    NSMutableArray *tempArrayForPic = [NSMutableArray array];
    for (NSObject *subArrayObject in self.pic) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForPic addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForPic addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForPic] forKey:kSquareDetailsPostPic];
    [mutableDict setValue:self.quotecount forKey:kSquareDetailsPostQuotecount];
    [mutableDict setValue:self.avatar forKey:kSquareDetailsPostAvatar];
    [mutableDict setValue:self.dateline forKey:kSquareDetailsPostDateline];
    NSMutableArray *tempArrayForQuote = [NSMutableArray array];
    for (NSObject *subArrayObject in self.quote) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForQuote addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForQuote addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForQuote] forKey:kSquareDetailsPostQuote];
    [mutableDict setValue:self.pid forKey:kSquareDetailsPostPid];
    [mutableDict setValue:self.tid forKey:kSquareDetailsPostTid];
    [mutableDict setValue:self.status forKey:kSquareDetailsPostStatus];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.message = [aDecoder decodeObjectForKey:kSquareDetailsPostMessage];
    self.author = [aDecoder decodeObjectForKey:kSquareDetailsPostAuthor];
    self.useip = [aDecoder decodeObjectForKey:kSquareDetailsPostUseip];
    self.authorid = [aDecoder decodeObjectForKey:kSquareDetailsPostAuthorid];
    self.pic = [aDecoder decodeObjectForKey:kSquareDetailsPostPic];
    self.quotecount = [aDecoder decodeObjectForKey:kSquareDetailsPostQuotecount];
    self.avatar = [aDecoder decodeObjectForKey:kSquareDetailsPostAvatar];
    self.dateline = [aDecoder decodeObjectForKey:kSquareDetailsPostDateline];
    self.quote = [aDecoder decodeObjectForKey:kSquareDetailsPostQuote];
    self.pid = [aDecoder decodeObjectForKey:kSquareDetailsPostPid];
    self.tid = [aDecoder decodeObjectForKey:kSquareDetailsPostTid];
    self.status = [aDecoder decodeObjectForKey:kSquareDetailsPostStatus];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_message forKey:kSquareDetailsPostMessage];
    [aCoder encodeObject:_author forKey:kSquareDetailsPostAuthor];
    [aCoder encodeObject:_useip forKey:kSquareDetailsPostUseip];
    [aCoder encodeObject:_authorid forKey:kSquareDetailsPostAuthorid];
    [aCoder encodeObject:_pic forKey:kSquareDetailsPostPic];
    [aCoder encodeObject:_quotecount forKey:kSquareDetailsPostQuotecount];
    [aCoder encodeObject:_avatar forKey:kSquareDetailsPostAvatar];
    [aCoder encodeObject:_dateline forKey:kSquareDetailsPostDateline];
    [aCoder encodeObject:_quote forKey:kSquareDetailsPostQuote];
    [aCoder encodeObject:_pid forKey:kSquareDetailsPostPid];
    [aCoder encodeObject:_tid forKey:kSquareDetailsPostTid];
    [aCoder encodeObject:_status forKey:kSquareDetailsPostStatus];
}

- (id)copyWithZone:(NSZone *)zone
{
    SquareDetailsPost *copy = [[SquareDetailsPost alloc] init];
    
    if (copy) {

        copy.message = [self.message copyWithZone:zone];
        copy.author = [self.author copyWithZone:zone];
        copy.useip = [self.useip copyWithZone:zone];
        copy.authorid = [self.authorid copyWithZone:zone];
        copy.pic = [self.pic copyWithZone:zone];
        copy.quotecount = [self.quotecount copyWithZone:zone];
        copy.avatar = [self.avatar copyWithZone:zone];
        copy.dateline = [self.dateline copyWithZone:zone];
        copy.quote = [self.quote copyWithZone:zone];
        copy.pid = [self.pid copyWithZone:zone];
        copy.tid = [self.tid copyWithZone:zone];
        copy.status = [self.status copyWithZone:zone];
    }
    
    return copy;
}


@end
